#ifndef PROGRAMME_LIB_H_
#define PROGRAMME_LIB_H_

#include <cstring>
#include <stdio.h>
#include <stdlib.h>
#include <fstream>
#include <sstream>

#include "Programme.h"
#include "Subject_lib.h"


void Add_programme(string s, Programme l[], int &numberProg){
    string a_p_id, a_p_name, temp, str_temp;
	string [30] s_elec_id, s_comp_id;
    int a_credit, mark2 = 0, count = 0;

    int mark = s.find("|");
    a_p_id = s.substr(0,mark);

    temp = s.substr(mark+1);
    mark = temp.find("|");
    a_p_name = temp.substr(0,mark);

	//get array of compulsory subjects
    temp = s.substr(mark+1);
	mark = temp.find("|");
	str_temp = temp.substr(0,mark);
	while(str_temp.length() != 0){
		mark2 = str_temp.find(":");
		s_comp_id[count] = str_temp.substr(0, mark2);
		str_temp = str_temp.substr(mark2 + 1);
	}
	
	//get array of elective subjects
    temp = s.substr(mark+1);
	mark = temp.find("|");
	str_temp = temp.substr(0,mark);
	while(str_temp.length() != 0){
		mark2 = str_temp.find(":");
		s_comp_id[count] = str_temp.substr(0, mark2);
		str_temp = str_temp.substr(mark2 + 1);
	}
	
    temp = temp.substr(mark+1);
    mark = temp.find("|");
    a_credit = atoi(temp.substr(0,mark).c_str());

    Programme prog(a_id, a_sname, a_stype, a_credit, a_presub, a_percent_midterm);
    l[numberProg] = prog;
    numberProg = numberProg + 1;
}

void Read_programme(string _file, Programme l[], int &numberProg){
    ifstream fin;
    fin.open(_file.c_str());
    if(!fin.is_open()){
        cout<<"Cannot open file.\n";
        exit(1);
    }else{
        string s = "";
        while (getline(fin, s)){
            Add_programme(s,l,numberProg);
        }
        fin.close();
    }
}

Programme Get_programme_by_id(string key, Programme l[], int numberProg){
    int i = 0;

    for (i = 0; i < numberSubject; i++){
        if (l[i].Get_p_id() == key){
            return l[i];
        }
    }
}


#endif